package com.zwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zwj.bo.CourseIndustryBo;
import com.zwj.dto.PaginationDate;
import com.zwj.dto.Result;
import com.zwj.dto.ReturnDate;
import com.zwj.entity.CourseIndustryEntity;
import com.zwj.enums.IsDeletedEnum;
import com.zwj.enums.StatusEnum;
import com.zwj.mapper.CourseIndustryMapper;
import com.zwj.service.CourseIndustryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.DataCheckUtils;
import com.zwj.utils.PaginationUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-11-02
 */
@Service
public class CourseIndustryServiceImpl extends ServiceImpl<CourseIndustryMapper, CourseIndustryEntity> implements CourseIndustryService {

    private final CourseIndustryMapper courseIndustryMapper;
    public CourseIndustryServiceImpl(CourseIndustryMapper courseIndustryMapper){
        this.courseIndustryMapper=courseIndustryMapper;
    }
    @Override
    public Result selectIndustry(CourseIndustryBo courseIndustryBo) {
        //数据校验
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(courseIndustryBo.getNumber(), courseIndustryBo.getPage()), "分页参数错误!");
        int page = courseIndustryBo.getPage();
        courseIndustryBo.setPage(PaginationUtils.getPageIndex(courseIndustryBo.getPage(), courseIndustryBo.getNumber()));
        //查询行业
        List<Object> result = courseIndustryMapper.selectIndustry(courseIndustryBo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(courseIndustryBo.getNumber(), page, sum);
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        List<CourseIndustryEntity> courseIndustryEntities =  (List<CourseIndustryEntity>) result.get(0);
        return Result.ok().data(new ReturnDate(paginationDate,courseIndustryEntities));
    }

    @Override
    public Result addIndustry(CourseIndustryEntity courseIndustryEntity) {
        courseIndustryEntity.setIsDeleted(IsDeletedEnum.NO.getKey());
        courseIndustryEntity.setStatus(StatusEnum.EFFECTIVE.getKey());
        int row = courseIndustryMapper.insert(courseIndustryEntity);
        if (row==0){
            return Result.error().message("添加行业失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateIndustry(CourseIndustryEntity courseIndustryEntity) {
        UpdateWrapper<CourseIndustryEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseIndustryEntity::getId,courseIndustryEntity.getId());
        int row = courseIndustryMapper.update(courseIndustryEntity,updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑批次信息失败");
        }
        return Result.ok();
    }

    @Override
    public Result deleteIndustry(CourseIndustryEntity courseIndustryEntity) {
        DataCheckUtils.isNotBlank(courseIndustryEntity.getId(), "行业id不能为空!");
        courseIndustryEntity.setIsDeleted(IsDeletedEnum.YES.getKey());
        UpdateWrapper<CourseIndustryEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseIndustryEntity::getId,courseIndustryEntity.getId());
        int row = courseIndustryMapper.update(courseIndustryEntity,updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除行业失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateStatus(CourseIndustryEntity courseIndustryEntity) {
        DataCheckUtils.isNotBlank(courseIndustryEntity.getId(), "行业id不能为空!");
        courseIndustryEntity.setStatus(courseIndustryEntity.getStatus());
        UpdateWrapper<CourseIndustryEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseIndustryEntity::getId,courseIndustryEntity.getId());
        int row = courseIndustryMapper.update(courseIndustryEntity,updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("修改状态行业失败");
        }
        return Result.ok();
    }


}
